import { reactive } from "vue";
import type { FormRules } from "element-plus";

/** 单个记录表单规则 */
export const formRules = reactive(<FormRules>{
  user_id: [{ required: true, message: "请选择投放人员", trigger: "change" }],
  consumption_date: [{ required: true, message: "请选择消耗日期", trigger: "change" }],
  ad_plan_id: [{ required: true, message: "请选择广告计划", trigger: "change" }],
  agent_id: [{ required: true, message: "请选择代理商", trigger: "change" }],
  total_amount: [
    { required: true, message: "消耗金额为必填项", trigger: "blur" },
    { 
      pattern: /^(0|[1-9]\d*)(\.\d{1,2})?$/, 
      message: "金额格式不正确，最多保留两位小数", 
      trigger: "blur" 
    },
    {
      validator: (rule, value, callback) => {
        if (value <= 0) {
          callback(new Error("消耗金额必须大于0"));
        } else if (value > 999999999.99) {
          callback(new Error("消耗金额不能超过999999999.99"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
});

/** 批量录入表单规则 */
export const batchFormRules = reactive(<FormRules>{
  user_id: [{ required: true, message: "请选择投放人员", trigger: "change" }],
  consumption_date: [{ required: true, message: "请选择消耗日期", trigger: "change" }],
  ad_plan_id: [{ required: true, message: "请选择广告计划", trigger: "change" }],
  input_type: [{ required: true, message: "请选择录入方式", trigger: "change" }],
  manual_data: [
    {
      validator: (rule, value, callback) => {
        if (!value || value.length === 0) {
          callback(new Error("请至少添加一条手工录入数据"));
        } else {
          // 检查每一项的必填字段
          for (let i = 0; i < value.length; i++) {
            const item = value[i];
            if (!item.agent_id) {
              callback(new Error(`第${i + 1}行：请选择代理商`));
              return;
            }
            if (!item.amount || item.amount <= 0) {
              callback(new Error(`第${i + 1}行：请输入正确的消耗金额`));
              return;
            }
            if (item.amount > 999999999.99) {
              callback(new Error(`第${i + 1}行：消耗金额不能超过999999999.99`));
              return;
            }
          }
          
          // 检查重复的代理商
          const agentIds = value.map(item => item.agent_id).filter(id => id);
          const uniqueAgentIds = [...new Set(agentIds)];
          if (agentIds.length !== uniqueAgentIds.length) {
            callback(new Error("存在重复的代理商，请检查"));
            return;
          }
          
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  account_data: [
    {
      validator: (rule, value, callback) => {
        if (!value || value.length === 0) {
          callback(new Error("请至少添加一个账号"));
        } else {
          // 检查每一项的必填字段
          for (let i = 0; i < value.length; i++) {
            const item = value[i];
            if (!item.account_id) {
              callback(new Error(`第${i + 1}行：请选择广告账号`));
              return;
            }
            if (!item.amount || item.amount <= 0) {
              callback(new Error(`第${i + 1}行：请输入正确的消耗金额`));
              return;
            }
            if (item.amount > 999999999.99) {
              callback(new Error(`第${i + 1}行：消耗金额不能超过999999999.99`));
              return;
            }
          }
          
          // 检查重复的账号
          const accountIds = value.map(item => item.account_id).filter(id => id);
          const uniqueAccountIds = [...new Set(accountIds)];
          if (accountIds.length !== uniqueAccountIds.length) {
            callback(new Error("存在重复的广告账号，请检查"));
            return;
          }
          
          callback();
        }
      },
      trigger: "blur"
    }
  ]
}); 